import React ,{useEffect,useRef} from 'react'
import * as echarts from 'echarts';
import {get_charts} from "../../api/index"
import {useDispatch,useSelector} from "react-redux"
import * as type from "../../utils/index"
type Props = {}

const Ios = (props: Props) => {
  const my_chart:any = useRef()
  const pie_chart:any = useRef()
  const dispatch:type.RootDispatch = useDispatch()
  const {charts_data} = useSelector((state:type.RootState)=>state)
  useEffect(()=>{
    dispatch(get_charts())
    // 基于准备好的dom，初始化echarts实例
    let myChart = echarts.init(my_chart.current);
    let pie_charts = echarts.init(pie_chart.current);
    let option1 = {
      title: {
        text: 'Referer of a Website',
        subtext: 'Fake Data',
        left: 'center'
      },
      tooltip: {
        trigger: 'item'
      },
      legend: {
        orient: 'vertical',
        left: 'left'
      },
      series: [
        {
          name: 'Access From',
          type: 'pie',
          radius: '50%',
          data: [
            { value: 1048, name: 'Search Engine' },
            { value: 735, name: 'Direct' },
            { value: 580, name: 'Email' },
            { value: 484, name: 'Union Ads' },
            { value: 300, name: 'Video Ads' }
          ],
          emphasis: {
            itemStyle: {
              shadowBlur: 10,
              shadowOffsetX: 0,
              shadowColor: 'rgba(0, 0, 0, 0.5)'
            }
          }
        }
      ]
    }
    let option = {
      tooltip: {},
      xAxis: {
        data: charts_data.title
      },
      yAxis: {},
      series: [
        {
          name: '销量',
          type: 'line',
          data: charts_data.value
        }
      ]
    }
    pie_charts.setOption(option1)
    charts_data.title&&myChart.setOption(option)
    return () =>{
      myChart.dispose()
      pie_charts.dispose()
    }
  },[dispatch])
  return (
    <div>
      <div style={{width:"400px",height:"400px"}} ref={my_chart}></div>
      <div style={{width:"400px",height:"400px"}} ref={pie_chart}></div>
    </div>
  )
}

export default Ios